package com.jsb.laundry.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsb.laundry.common.exception.*;
import com.jsb.laundry.model.dto.posts.PostFavourQueryRequest;
import com.jsb.laundry.model.dto.posts.PostLikeAddRequest;
import com.jsb.laundry.model.dto.posts.PostLikeQueryRequest;
import com.jsb.laundry.model.entity.JsbPosts;
import com.jsb.laundry.model.vo.PostsVO;
import com.jsb.laundry.service.JsbPostLikesService;
import com.jsb.laundry.service.JsbPostsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子点赞接口
 */
@RestController
@RequestMapping("/posts/like")
@Api(value = "帖子相关接口",tags = "帖子")
@Slf4j
public class PostLikeController {

    @Resource
    private JsbPostLikesService postLikeService;

    @Resource
    private JsbPostsService postService;

    /**
     * 点赞 / 取消点赞
     *
     * @param postLikeAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/op")
    @ApiOperation("帖子 点赞 / 取消点赞")
    public BaseResponse<Integer> doLike(@RequestBody PostLikeAddRequest postLikeAddRequest,
                                        HttpServletRequest request) {
        if (postLikeAddRequest == null || postLikeAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userId = postLikeAddRequest.getUserId();
        Integer postId = postLikeAddRequest.getPostId();
        int result = postLikeService.doPostLike(postId, userId);
        return ResultUtils.success(result);
    }

    /**
     * 获取用户点赞的帖子列表
     *
     * @param postLikeQueryRequest
     * @param request
     */
    @PostMapping("/user/list")
    @ApiOperation("获取用户点赞的帖子列表")
    public BaseResponse<Page<PostsVO>> listLikesPostByPage(@RequestBody PostLikeQueryRequest postLikeQueryRequest,
                                                            HttpServletRequest request) {
        if (postLikeQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if ("".equals(postLikeQueryRequest.getUserId())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户ID 不能为空");
        }
        long current = postLikeQueryRequest.getPageNum();
        long size = postLikeQueryRequest.getPageSize();
        String userId = postLikeQueryRequest.getUserId();
        String loginUserId = postLikeQueryRequest.getPostQueryRequest().getLoginUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<JsbPosts> postPage = postLikeService.listLikePostByPage(new Page<>(current, size),
                postService.getQueryWrapper(postLikeQueryRequest.getPostQueryRequest()), userId);
        return ResultUtils.success(postService.getPostVOPage(loginUserId, postPage, request));
    }

}
